<?php

namespace Biz\Player;

use Base\Exception\NoRecordException;

class Player extends \Base\BaseBiz
{
    //更新信息
    public static function update($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $player_orm = new \Orm\Player\PlayerClass();
            if(!isset($_orm['id']) || !$_orm['id']){
                throw new \Exception('数据错误');
            }
            $player_orm['id'] = $_orm['id'];
            $player_orm['player_name'] = $_orm['player_name']??'';
            $player_orm['icon_url'] = $_orm['icon_url']??'';
            $player_orm['description'] = $_orm['description']??'';
            $player_orm['play_times'] = $_orm['play_times']??'';
            $player_orm['score'] = $_orm['score']??'';
            $player_orm['win_times'] = $_orm['win_times']??'';
            $player_orm['lose_times'] = $_orm['lose_times']??'';
            $player_orm['ranking'] = $_orm['ranking']??'';
            $is_update = $player_orm->update();
            if($is_update){
                $rs['isok'] = true;
            }else{
                throw new \Exception('更新失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    /**
     * 获取信息
     */
    public static function getInfo($id)
    {
        $player_orm = new \Orm\Player\PlayerClass();
        $player_orm->loadById($id);

        return $player_orm;
    }

    /*
     * 获取列表
     */
    public static function getList($where, $now = 1, $size = 0)
    {
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $player_list = new \Orm\Player\PlayerList();
        if (!empty($where)) {
            $player_list['where'] = $where;
        } else {
            $player_list['where'] = '';
        }
        $player_list['limit'] = $size;
        $player_list['offset'] = ($now - 1) * $size;
        $player_list['order'] = '`insert_time` ASC';
        $player_list['key_by'] = 'id';
        $player_list->load();

        $result = array();
        $result['lists'] = $player_list['lists'];
        $result['total'] = $player_list['total'];
        return $result;
    }
}
